﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace SharpSoft.WPF.Controls
{
    public abstract class ValidationBase : System.Windows.Controls.ValidationRule
    {
        public string errortext { get; set; }

        public virtual string GetErrorMessage()
        {
            return errortext;
        }

        public object contextModel { get; set; }

        protected ValidationResult fail()
        {
            return new ValidationResult(false, GetErrorMessage());
        }
        protected ValidationResult sucess()
        {
            return new ValidationResult(true, null);
        }
    }
    /// <summary>
    /// 这个字段必须填入任意值
    /// </summary>
    public class required : ValidationBase
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            if (value == null)
            {
                return fail();
            }
            if (value is string)
            {
                if (string.IsNullOrWhiteSpace((string)value))
                {
                    return fail();
                }
            }
            return sucess();
        }
    }

    /// <summary>
    /// 限定字符串长度
    /// </summary>
    public class length : ValidationBase
    {
        public int min { get; set; }

        public int max { get; set; }
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            int len = 0;
            if (value != null)
            {
                len = value.ToString().Length;
            }
            if (min == -1)
            {
                min = int.MinValue;
            }
            if (max == -1)
            {
                max = int.MaxValue;
            }
            if (len >= min && len <= max)
            {
                return sucess();
            }
            return fail();
        }
    }
    [TypeConverter(typeof(RangeTypeConverter))]
    public  class Range
    {
        public decimal Min { get; set; }
        public decimal Max { get; set; }
    }
    public class RangeTypeConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return sourceType == typeof(Range) || sourceType == typeof(string);
        }
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is Range)
            {
                return (Range)value;
            }
            else if (value is string str)
            {
                try
                {
                    var ss = str.Split(',');
                    if (ss.Length!=2)
                    {
                        throw new Exception("Range的字符串格式不对。");
                    }
                    return new Range() { Min = decimal.Parse(ss[0]), Max = decimal.Parse(ss[1]) };
                }
                catch (Exception)
                {

                    throw;
                }
            }
            return base.ConvertFrom(context, culture, value);
        }
    }

    /// <summary>
    /// 限定数值类型的范围
    /// </summary>
    public class range : ValidationBase
    {
        public range()
        {
            min = int.MinValue;
            max = int.MaxValue;
        }
        public decimal min { get; set; }

        public decimal max { get; set; }
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            if (value == null)
            {
                return fail();
            }
            string str = value.ToString();
            decimal d;
            if (decimal.TryParse(str, out d))
            {
                if (d <= max && d >= min)
                {
                    return sucess();
                }
            }
            return fail();
        }
    }
    /// <summary>
    /// 验证正则表达式
    /// </summary>
    public class regex : ValidationBase
    {
        /// <summary>
        /// 表达式
        /// </summary>
        public string expression { get; set; }
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            string str;
            if (value == null)
            {
                str = "";
            }
            else
            {
                str = value.ToString();
            }
            Regex reg = new Regex(expression);
            var ismatch = reg.IsMatch(str);
            if (ismatch)
            {
                return sucess();
            }
            else
            {
                return fail();
            }
        }
    }

    public class custom : ValidationBase
    {

        public UIElement uiele { get; set; }
        public customRoutedEventArgs EventArgs { get; set; }
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            EventArgs.Value = value;
            EventArgs.IsValidated = true;
            uiele.RaiseEvent(EventArgs);

            if (EventArgs.IsValidated)
            {
                return sucess();
            }
            else
            {
                return fail();
            }
        }
    }
    public class customRoutedEventArgs : RoutedEventArgs
    {
        public object Value { get; internal set; }
        public bool IsValidated { get; set; }
        protected override void InvokeEventHandler(Delegate genericHandler, object genericTarget)
        {
            base.InvokeEventHandler(genericHandler, genericTarget);
        }
    }
}
